package com.geekaca.iobuffer.work1;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 统计单词数量
public class Test1 {
    public static void main(String[] args) {

        Map<String, Integer> wordsMap = new HashMap<>();

        try (Reader fr = new FileReader("studyIOBuffer\\THIRDPARTYLICENSEREADME-JAVAFX.txt")) {
            // 创建字符缓冲流
            BufferedReader br = new BufferedReader(fr);
            Pattern pattern = Pattern.compile("[a-zA-Z]+");
            String line = null;
            while ((line = br.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                while (matcher.find()) {
                    String word = matcher.group();
                    // 找到的词放入map集合 ,如果map没有该键,就创建一个键,值设置为1,如果有该键,就把对应的值+1
                    if (!wordsMap.containsKey(word)) {
                        wordsMap.put(word, 1);
                    } else {
                        wordsMap.put(word, wordsMap.get(word) + 1);
                    }
                }
            }
            // 最后查看map,即可查看每个词对应的数量
            System.out.println(wordsMap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
